package io;

import java.io.*;
import java.util.Scanner;

/**
 * @author haomin
 * @date 2022/05/09 20:37
 **/
public class ReplaceTool {
//    public static void main(String[] args) throws IOException {
//        // 读取对象
//        try (InputStream is = new FileInputStream("D:\\电影\\文件操作\\it.txt")){
//            try (Scanner scanner = new Scanner(is)){
//                // 输出对象
//                try (OutputStream os = new FileOutputStream("D:\\电影\\文件操作\\it.txt")){
//                    try (Writer writer = new OutputStreamWriter(os)){
//                        try (PrintWriter printWriter = new PrintWriter(writer)){
//                            StringBuffer res = new StringBuffer();
//                            while (scanner.hasNextLine()){
//                                String line = scanner.nextLine();
//                                res.append(line.replace("1", "a").replace("2", "b")
//                                        .replace("3", "c").replace("4", "d")).append("\r\n");
//                            }
//                            System.out.println(res);
//                            printWriter.println(res);
//                            printWriter.flush();
//                        }
//                    }
//                }
//            }
//        }
//    }
//}


    public static void main(String[] args) throws IOException {
        // 统一读取对象
        File file = new File("some file path");
        StringBuffer s = new StringBuffer();
        try (InputStream is = new FileInputStream(file)){
            try (Scanner scanner = new Scanner(is)){
                while (scanner.hasNextLine()) {
                    String line = scanner.nextLine();
                    // 字符串替换
                    s.append(line.replace("1", "a").replace("2", "b")
                            .replace("3", "c").replace("4", "d")).append("\r\n");

                    System.out.println("Debug: " + s);
                }
            }
        }
        // 统一输出对象
        try (OutputStream os = new FileOutputStream(file)){
            try (Writer writer = new OutputStreamWriter(os)){
                try (PrintWriter printWriter = new PrintWriter(writer)){
                    printWriter.println(s);
                    printWriter.flush();
                }
            }
        }
    }
}